/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DTO.TinTucDTO;
import doanjava.DataProvider;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Mr_Quay
 */
public class TinTucDAO {

    public static void InsertTinTuc(DTO.TinTucDTO dto) throws SQLException {
        DataProvider pro = new DataProvider();
        pro.open();
        Connection con = pro.getCon();
        Statement st = con.createStatement();
        String sql = "Insert into TinTuc values(?,?,?,?,?,?)";
        PreparedStatement pr = con.prepareStatement(sql);
        pr.setString(1, dto.getTenweb());
        pr.setString(2, dto.getTieude());
        pr.setString(3, dto.getLink());
        pr.setString(4, dto.getNgaydang());
        pr.setString(5, dto.getHinhanh());
        pr.setString(6, dto.getTomtat());
        int rs = pr.executeUpdate();
        pro.close();

    }

    public static List<DTO.TinTucDTO> layDSTinTuc(String tenweb) throws SQLException, IOException, ClassNotFoundException {
        DataProvider pro = new DataProvider();
        pro.open();
        Connection con = pro.getCon();
        Statement st = con.createStatement();
        String strSQL = "select * from tintuc where TenWeb=?";
        PreparedStatement pr = con.prepareStatement(strSQL);
        pr.setString(1, tenweb);
        ResultSet rs = pr.executeQuery();
        List<DTO.TinTucDTO> lst = new ArrayList<>();
        while (rs.next()) {
            String strtenweb = rs.getString("TenWeb");
            DTO.TinTucDTO dto = new TinTucDTO(strtenweb);
            dto.setTieude(rs.getString("Title"));
            dto.setLink(rs.getString("Link"));
            dto.setNgaydang(rs.getString("Date"));
            dto.setHinhanh(rs.getString("Image"));
            dto.setTomtat(rs.getString("Tomtat"));
            lst.add(dto);
        }
        con.close();
        return lst;
    }
}
